﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class NurseDataFactory
    {
        public static NurseData Construct(IDataReader reader)
        {
            NurseData data = new NurseData();

            try { data.ID = Convert.ToString(reader["ID"]); }
            catch { }

            try { data.Name = Convert.ToString(reader["Name"]); }
            catch { }

            try { data.Sex = Convert.ToString(reader["Sex"]); }
            catch { }

            try { data.Nation = Convert.ToString(reader["Nation"]); }
            catch { }

            try { data.Born = Convert.ToString(reader["Born"]); }
            catch { }

            try { data.Address = Convert.ToString(reader["Address"]); }
            catch { }

            try { data.GrantDept = Convert.ToString(reader["GrantDept"]); }
            catch { }

            try { data.UserLifeBegin = Convert.ToString(reader["UserLifeBegin"]); }
            catch { }

            try { data.UserLifeEnd = Convert.ToString(reader["UserLifeEnd"]); }
            catch { }

            try { data.PhoneNum = Convert.ToString(reader["PhoneNum"]); }
            catch { }

            try { data.TEID = Convert.ToString(reader["TEID"]); }
            catch { }

            return data;
        }
        public static String GetPrimaryKey(DataTable dt)
        {
            return GetPrimaryKey(dt, 0);
        }
        public static String GetPrimaryKey(DataTable dt, int rowIndex)
        {
            String PrimaryKey = "";

            if (dt.Rows.Count < rowIndex + 1)

                return "";

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return "";

            try { PrimaryKey = Convert.ToString(dr[NurseData.PRIMARYKEY_Field]); }
            catch { }

            return PrimaryKey;
        }
        public static NurseData Construct(DataTable dt)
        {
            return Construct(dt, 0);
        }
        public static NurseData Construct(DataTable dt, int rowIndex)
        {
            NurseData data = new NurseData();

            if (dt.Rows.Count < rowIndex + 1)

                return null;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return null;

            try { data.ID = Convert.ToString(dr["ID"]); }
            catch { }

            try { data.Name = Convert.ToString(dr["Name"]); }
            catch { }

            try { data.Sex = Convert.ToString(dr["Sex"]); }
            catch { }

            try { data.Nation = Convert.ToString(dr["Nation"]); }
            catch { }

            try { data.Born = Convert.ToString(dr["Born"]); }
            catch { }

            try { data.Address = Convert.ToString(dr["Address"]); }
            catch { }

            try { data.GrantDept = Convert.ToString(dr["GrantDept"]); }
            catch { }

            try { data.UserLifeBegin = Convert.ToString(dr["UserLifeBegin"]); }
            catch { }

            try { data.UserLifeEnd = Convert.ToString(dr["UserLifeEnd"]); }
            catch { }

            try { data.PhoneNum = Convert.ToString(dr["PhoneNum"]); }
            catch { }

            try { data.TEID = Convert.ToString(dr["TEID"]); }
            catch { }

            return data;
        }
        public static NurseData Construct(DataRow dr)
        {
            NurseData data = new NurseData();

            try { data.ID = Convert.ToString(dr["ID"]); }
            catch { }

            try { data.Name = Convert.ToString(dr["Name"]); }
            catch { }

            try { data.Sex = Convert.ToString(dr["Sex"]); }
            catch { }

            try { data.Nation = Convert.ToString(dr["Nation"]); }
            catch { }

            try { data.Born = Convert.ToString(dr["Born"]); }
            catch { }

            try { data.Address = Convert.ToString(dr["Address"]); }
            catch { }

            try { data.GrantDept = Convert.ToString(dr["GrantDept"]); }
            catch { }

            try { data.UserLifeBegin = Convert.ToString(dr["UserLifeBegin"]); }
            catch { }

            try { data.UserLifeEnd = Convert.ToString(dr["UserLifeEnd"]); }
            catch { }

            try { data.PhoneNum = Convert.ToString(dr["PhoneNum"]); }
            catch { }

            try { data.TEID = Convert.ToString(dr["TEID"]); }
            catch { }

            return data;
        }
        public static String GetPrimaryKey(DataRow dr)
        {
            String PrimaryKey = "";

            try { PrimaryKey = Convert.ToString(dr[NurseData.PRIMARYKEY_Field]); }
            catch { }
            return PrimaryKey;
        }

    }
    public class NurseDataDBOption
    {
        public static int Insert(NurseData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("insert into Nurse (");
            sql.Append("ID,Name, ");
            sql.Append("Sex, ");
            sql.Append("Nation, ");
            sql.Append("Born, ");
            sql.Append("Address, ");
            sql.Append("GrantDept, ");
            sql.Append("UserLifeBegin, ");
            sql.Append("UserLifeEnd, ");
            sql.Append("PhoneNum, ");
            sql.Append("TEID");
            sql.Append(") values (");
            sql.Append("'");
            sql.Append(d.ID.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.Name.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.Sex.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.Nation.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.Born.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.Address.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.GrantDept.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.UserLifeBegin.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.UserLifeEnd.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.PhoneNum.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.TEID.ToString());
            sql.Append("'");
            sql.Append(")");
            return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
        }
        public static int Update(NurseData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("update Nurse set ");
            sql.Append("Name=");
            sql.Append("'");
            sql.Append(d.Name.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("Sex=");
            sql.Append("'");
            sql.Append(d.Sex.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("Nation=");
            sql.Append("'");
            sql.Append(d.Nation.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("Born=");
            sql.Append("'");
            sql.Append(d.Born.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("Address=");
            sql.Append("'");
            sql.Append(d.Address.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("GrantDept=");
            sql.Append("'");
            sql.Append(d.GrantDept.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("UserLifeBegin=");
            sql.Append("'");
            sql.Append(d.UserLifeBegin.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("UserLifeEnd=");
            sql.Append("'");
            sql.Append(d.UserLifeEnd.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("PhoneNum=");
            sql.Append("'");
            sql.Append(d.PhoneNum.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("TEID=");
            sql.Append("'");
            sql.Append(d.TEID.ToString());
            sql.Append("'");
            sql.Append(" where ID=");
            sql.Append("'");
            sql.Append(d.ID.ToString()); sql.Append("'");

            return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
        }
        public static int Delete(NurseData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("delete from Nurse ");
            sql.Append(" where ID=");
            sql.Append("'");
            sql.Append(d.ID.ToString());
            sql.Append("'");
            return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
        }
        public static DataTable DataTableSelect()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from Nurse ");
            return WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
        }
        public static DataTable DataTableSelect(NurseData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from Nurse where ID=");
            sql.Append("'");
            sql.Append(d.ID.ToString());
            sql.Append("'");
            return WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
        }
        public static NurseData Get(String _id)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from Nurse where ID=");
            sql.Append("'");
            sql.Append(_id.ToString());
            sql.Append("'");
            DataTable dt = WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
            if (dt.Rows.Count == 0)
                return null;
            else
                return NurseDataFactory.Construct(dt.Rows[0]);
        }        
        public static DataSet DataSetSelect()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from Nurse ");
            return WFNetLib.ADO.SQLServerOP.DataSetSQL(sql.ToString());
        }
        public static SqlDataReader ReaderSelect(NurseData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from Nurse ");
            return WFNetLib.ADO.SQLServerOP.ReaderExecuteSQL(sql.ToString());
        }
        public static DataTable GetByTEID(String _id)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from Nurse where TEID=");
            sql.Append("'");
            sql.Append(_id.ToString());
            sql.Append("'");
            DataTable dt = WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
            return dt;
        }
    }
}
